import re

FIRST_CAP_RE = re.compile("(.)([A-Z][a-z]+)")
ALL_CAP_RE = re.compile("([a-z0-9])([A-Z])")


def snake_case(camel_str):
    s = FIRST_CAP_RE.sub(r"\1_\2", camel_str)
    return ALL_CAP_RE.sub(r"\1_\2", s).lower()


def camel_case(snake_str, lower_head=True):
    head, *tail = snake_str.split("_")

    if lower_head:
        head = head.lower()
    else:
        head = head.title()

    return head + "".join(x.title() for x in tail)
